/*张欢欢 设备类型页面*/
var totalPage;
var totalCnt;
var deleteFlag;
var repairFauleHistoriesDate = [];

$(document).ready(function() {
    showLoading();
    $('title', parent.document).html($('title').text());
    selectDate("startDate","0");
	selectDate("endDate","0");
    setBaseUrl();
    setTimeout(hideLoading, 500);
});

function searchRepairRecord(){
    $("#page").val(1);
    $("#currentPage").val(1);
    search();
}

//故障维修记录查询
function search (){
    showLoading();
    seach = 1;
    $("#chkAll").attr("checked", false);
    $.ajax({
        type : 'post',
        data : $("#searchBreakRepairRecord").serialize(),
        url : 'searchBreakRepairRecord',
        dataType : 'json',
        success : function(data){
            setTimeout(hideLoading, 500);
            clearTable("repairRecordTbody")
            var page = $('#page').val();
            var rows = $('#rows').val();
            totalPage = data && data.totalPage || 0;
            totalCnt = data && data.totalCnt || 0;
            repairFauleHistoriesDate = data && data.repairFauleHistoriesList || [];
            document.getElementById("total").innerText = '共'+totalCnt+'条/'+'共'+totalPage+'页';
            var tbody = $("#repairRecordTbody");
            // 当前页数
            var currentPage = $('#currentPage').val();
            // 每页显示的条数
            var rows = $('#rows').val();
            var length = repairFauleHistoriesDate.length;
            if (length == 0) {
                showMessge("没有查询结果");
            } else {
                for(var i=0; i<length; i++){
                    // 序号
                    var rowNo = (currentPage - 1) * rows + i + 1;
                    // 追加显示数据
                    var $tr = $('<tr>').appendTo(tbody);
                    $("<td>").html(rowNo).appendTo($tr);
                    $("<td>").html(repairFauleHistoriesDate[i].equipmentName).appendTo($tr);
                    $("<td>").html(repairFauleHistoriesDate[i].faultLocation).appendTo($tr);
                    $("<td>").html(repairFauleHistoriesDate[i].faultDescription).appendTo($tr);
                    $("<td>").html(timeStamp2String(repairFauleHistoriesDate[i].reportTime)).appendTo($tr);
                    $("<td>").html(timeStamp2String(repairFauleHistoriesDate[i].repairTime)).appendTo($tr);
                    $("<td>").html(repairFauleHistoriesDate[i].repairContent).appendTo($tr);
                    $("<td>").html(repairFauleHistoriesDate[i].repairResult).appendTo($tr);
                    $("<td>").html(repairFauleHistoriesDate[i].repairMan).appendTo($tr);
                    $("<td>").html(repairFauleHistoriesDate[i].chargingWorker).appendTo($tr);
                    $("<td>").html(repairFauleHistoriesDate[i].stationMaster).appendTo($tr);
                    $("<td>").html(timeStamp2String(repairFauleHistoriesDate[i].successTime)).appendTo($tr);
                    $("<td>").html(repairFauleHistoriesDate[i].remark).appendTo($tr);
                }
            }
        },
        error: function(data){
            setTimeout(hideLoading, 500);
            if (data.status == "403" && data.statusText == "Forbidden") {
                showMessge("没有访问权限");
            } else {
                showMessge("查询失败!");
            }
        },
        timeout: function(data){
            setTimeout(hideLoading, 500);
            showMessge("操作超时！");
        }
    });
}
//添加
function save(){
    $('#saveTitle').html('');
    var saveTitle = $('#saveTitle');
    var strong = document.createElement("strong");
    strong.innerHTML = '故障维修记录添加';
    selectDate("saveRepairDate","0");
    selectDate("saveServicingDate","0");
	selectDate("saveRecoveryDate","0");
    $("#save").modal("show");
    deleteFlag = "save";
    saveTitle.append(strong);
    formReset("saveForm");
}
function confirm(){
    var saveEquipmentName = $("#saveEquipmentName").val();
    var saveTroubleLocation = $("#saveTroubleLocation").val();
    var saveFaultDescription = $("#saveFaultDescription").val();
    var saveRepairTime = $("#saveRepairTime").val();
    var saveServicingTime = $("#saveServicingTime").val();
    var saveMaintenanceContent = $("#saveMaintenanceContent").val();
    var saveMaintenanceResults = $("#saveMaintenanceResults").val();
    var saveServiceMan = $("#saveServiceMan").val();
    var saveChargingMan = $("#saveChargingMan").val();
    var saveStationHead = $("#saveStationHead").val();
    var saveRecoveryTime = $("#saveRecoveryTime").val();
    var saveRemark = $("#saveRemark").val();
    if (!saveEquipmentName) {
        showMessge("设备名称不能为空");
        return;
    }
    if (!saveTroubleLocation) {
        showMessge("故障部位不能为空");
        return;
    }
    if (!saveFaultDescription) {
        showMessge("故障描述不能为空");
        return;
    }
    if (!saveRepairTime) {
        showMessge("报修时间不能为空");
        return;
    }
    if (!saveServicingTime) {
        showMessge("维修时间不能为空");
        return;
    }
    if (!saveMaintenanceContent) {
        showMessge("维修内容不能为空");
        return;
    }
    if (!saveMaintenanceResults) {
        showMessge("维修结果不能为空");
        return;
    }
    if (!saveServiceMan) {
        showMessge("维修人员不能为空");
        return;
    }
    if (!saveChargingMan) {
        showMessge("充电工不能为空");
        return;
    }
    if (!saveStationHead) {
        showMessge("站长不能为空");
        return;
    }
    if (!saveRecoveryTime) {
        showMessge("维修时间不能为空");
        return;
    }
    if (deleteFlag == "save"){
        showLoading();
        $.ajax({
            type: "POST",
            url: 'saveBreakRepairRecord',
            data: {
            	EquipmentName:saveEquipmentName,
            	FaultLocation:saveTroubleLocation,
            	FaultDescription:saveFaultDescription,
            	ReportDate:saveRepairTime,
            	RepairDate:saveServicingTime,
            	RepairContent:saveMaintenanceContent,
            	RepairResult:saveMaintenanceResults,
            	SuccessDate:saveRecoveryTime,
            	RepairMan:saveServiceMan,
            	ChargingWorker:saveChargingMan,
            	StationMaster:saveStationHead,
            	Remark:saveRemark,
            },
            dataType:'json',
            success: function(data){
                setTimeout(hideLoading, 500);
                $('#save').modal('hide');
                var result = data && data.result;
                if (result == "Success") {
                    showMessge("添加成功");
                } else if (result == "NotFound"){
                    showMessge("添加失败");
                } else {
                    showMessge("系统异常.请重试");
                }
                search();
                formReset("saveForm");
            },
            error : function(data) {
                setTimeout(hideLoading, 500);
                if (data.status == "403" && data.statusText == "Forbidden") {
                    showMessge("您没有操作权限!");
                } else {
                    showMessge("添加失败!");
                }
            },
            timeout: function(data){
                setTimeout(hideLoading, 500);
                showMessge("操作超时！");
            }
        });
}
}